//
//  AMAccountTool.m
//  AMwb
//
//  Created by zhc on 15/12/3.
//  Copyright © 2015年 zhc. All rights reserved.
//

#import "AMAccountTool.h"
#import "AMAccount.h"
#import "AMOAuthViewController.h"
#import "AMTabBarViewController.h"
#import "AMNewFeatureController.h"


#define AMaccountPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.plist"]
@implementation AMAccountTool


//
//+ (NSString *)path{
//
//  //  NSString * doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//   return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.plist"];
//
//
//}
//
//



+ (void)saveAccount:(AMAccount *)account{

    
//    //将返回的账号数据存进去沙盒
//    NSString * doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    NSString *path=[doc stringByAppendingPathComponent:@"account.plist"];
//    
//
//    
//    
//    //把得到的字典放到模型里面
//    AMAccount *account=[AMAccount accountWithDict:responseObject];
//    
    
    //自定义的对象存储 必须用NSkeyedArchiver ，没有什么writeToFile
    //产生的时间
     NSDate *createdTime =[NSDate date];
     account.created_Time=createdTime;
    
    
    [NSKeyedArchiver archiveRootObject:account toFile:AMaccountPath];
    NSLog(@"--%@",AMaccountPath);
    
    
    

}

+ (AMAccount *)account{
    
//    NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    
//    NSString *path=[doc stringByAppendingPathComponent:@"account.plist"];
    
    AMAccount *account=[NSKeyedUnarchiver unarchiveObjectWithFile:AMaccountPath];
    
    //验证账号是不是过期了
    long long expires_in=[account.expires_in longLongValue];
    
    //获得当前时间
    NSDate*now= [NSDate date];
    
    //获得过期时间
    NSDate *expiressTime=[account.created_Time dateByAddingTimeInterval:expires_in];
    //过期时间等于=创建时间+accesstoken的寿命  和当前的时间比较
    
    //expiressTime<now,过期了，降序和相等 没过期..
    // NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending 升 等 降
    NSComparisonResult result= [expiressTime compare:now];
  
    
    
    if(result != NSOrderedDescending){
    
        
        AMLog(@"过期了");
        return nil;
    
    }
    
    return account;
    AMLog(@"%@---%@",expiressTime,now);
    


}



//
//+(void)test{
//    
//    //从info中获得 版本号
//    
//    NSString *key=@"CFBundleVersion";
//    NSString *currentVerson =[NSBundle mainBundle].infoDictionary[key];
//    // NSLog(@"%@",[NSUserDefaults standardUserDefaults]);
//    //上一次的版本
//    NSString *lastKey=[[NSUserDefaults standardUserDefaults] objectForKey:key];
//    
//    
////    沙盒路径
////    
////    NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
////    
////    NSString *path=[doc stringByAppendingPathComponent:@"account.plist"];
////    
////    AMAccount *account=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
//    
//    //NSDictionary *account=[NSDictionary dictionaryWithContentsOfFile:path];
//    //获得window
//    
//    UIWindow *window=[UIApplication sharedApplication].keyWindow;
//   
//    if ([lastKey isEqualToString: currentVerson]) {//一样的版本号，直接进入主页
//        window.rootViewController = [[AMTabBarViewController alloc]init];
//    }else{ //不一样的版本号，新特性
//        
//        window.rootViewController = [[AMNewFeatureController alloc]init];
//        
//        //将新的版本号放进沙盒
//        [[NSUserDefaults standardUserDefaults] setObject:currentVerson forKey:key];
//        [[NSUserDefaults standardUserDefaults] synchronize];//马上同步沙盒信息
//        
//    }
//    
//    
//    
//
//
//}


@end
